//
//  ShoppingCartBottomView.m
//  MIT-AffordSuperMarket
//
//  Created by apple on 15/11/19.
//  Copyright © 2015年 河南大实惠电子商务有限公司. All rights reserved.
//

#import "ShoppingCartBottomView.h"
//untils
#import "UIImage+ColorToImage.h"
#import "UIColor+Hex.h"
#import "Global.h"
@interface ShoppingCartBottomView ()
@property(nonatomic, strong)UIButton *selectedAllProductBtn;//全选按钮
@property(nonatomic, strong)UILabel *totalPricelabel;//总价格
@property(nonatomic, strong)UIButton *goJieSuanBtn;//结算btn
@property(nonatomic, strong)UIButton *addMarkBtn;//添加关注btn
@property(nonatomic, strong)UIButton *deleteProductBtn;//删除品种btn
@end
@implementation ShoppingCartBottomView

- (id)initWithFrame:(CGRect)frame withEditStatus:(BOOL)isEdit {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubViewWithEditStatus:isEdit];
    }
    return self;
}
#pragma mark -- 初始化子视图
- (void)setupSubViewWithEditStatus:(BOOL)isEdit {
    //选中所有品种
    _selectedAllProductBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectedAllProductBtn.frame = CGRectMake(0
                                              ,0
                                              , 84
                                              , CGRectGetHeight(self.bounds));
    [_selectedAllProductBtn setTitle:@"全选" forState:UIControlStateNormal];
    _selectedAllProductBtn.titleLabel.font = [UIFont systemFontOfSize:FontSize(16)];
    [_selectedAllProductBtn setImage:[UIImage imageNamed:@"checkBoxNormalImage"] forState:UIControlStateNormal];
    [_selectedAllProductBtn setImage:[UIImage imageNamed:@"checkBoxSelectdImage"] forState:UIControlStateSelected];
    _selectedAllProductBtn.backgroundColor = [UIColor clearColor];
    [_selectedAllProductBtn addTarget:self action:@selector(selectAllProductEvent:) forControlEvents:UIControlEventTouchUpInside];
    _selectedAllProductBtn.imageEdgeInsets=UIEdgeInsetsMake(0, -20, 0, 0);
    _selectedAllProductBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    [self addSubview:_selectedAllProductBtn];
    //去结算按钮
    _goJieSuanBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    _goJieSuanBtn.frame = CGRectMake(CGRectGetWidth(self.bounds) - 100
                                              , 0
                                              , 100
                                              , CGRectGetHeight(self.bounds));
    [_goJieSuanBtn setTitle:@"去结算（0）" forState:UIControlStateNormal];
    UIImage *jieSuanBackgroundImage = [UIImage createImageWithColor:[UIColor colorWithHexString:@"#f15353"]
                                                              frame:CGRectMake(0, 0, CGRectGetWidth(_goJieSuanBtn.bounds), CGRectGetHeight(_goJieSuanBtn.bounds))];
    [_goJieSuanBtn setBackgroundImage:jieSuanBackgroundImage forState:UIControlStateNormal];
    _goJieSuanBtn.titleLabel.font = [UIFont systemFontOfSize:FontSize(18)];
    [_goJieSuanBtn addTarget:self action:@selector(goJieSuanEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_goJieSuanBtn];
    //总价格
    _totalPricelabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(_goJieSuanBtn.frame) - 18 - VIEW_WIDTH/3*2
                                                               ,0
                                                               , VIEW_WIDTH/3*2
                                                                , CGRectGetHeight(self.frame))];
    _totalPricelabel.textColor=[UIColor whiteColor];
    _totalPricelabel.textAlignment =NSTextAlignmentRight;
    _totalPricelabel.text = @"总计：￥0.00";
    _totalPricelabel.font = [UIFont systemFontOfSize:FontSize(17)];
    [self addSubview:_totalPricelabel];
    
    //编辑状态视图
    
    //移入关注
    UIImage *addMarkBackgroundImage = [UIImage createImageWithColor:[UIColor colorWithHexString:@"#eeeeee"] frame:CGRectMake(0, 0, 100, CGRectGetHeight(self.bounds))];
    
    _addMarkBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    _addMarkBtn.frame = CGRectMake(CGRectGetWidth(self.bounds) - 100*2 - 2*10
                                     , 5
                                     , 100
                                     , CGRectGetHeight(self.bounds)-10);
    [_addMarkBtn setTitle:@"移入关注" forState:UIControlStateNormal];
    [_addMarkBtn setBackgroundImage:addMarkBackgroundImage forState:UIControlStateNormal];
    _addMarkBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_addMarkBtn.layer setMasksToBounds:YES];
    [_addMarkBtn.layer setCornerRadius:4.0];
    [_addMarkBtn setTitleColor:[UIColor colorWithHexString:@"#979797"] forState:UIControlStateNormal];
    [_addMarkBtn addTarget:self action:@selector(addMarkEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_addMarkBtn];
    
    //删除
    UIImage *releteBackgroundImage = [UIImage createImageWithColor:[UIColor colorWithHexString:@"#da251c"] frame:CGRectMake(0, 0, 100, CGRectGetHeight(self.bounds))];
    _deleteProductBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteProductBtn.frame = CGRectMake(CGRectGetMaxX(_addMarkBtn.frame) + 10
                                   , CGRectGetMinY(_addMarkBtn.frame)
                                   , CGRectGetWidth(_addMarkBtn.bounds)
                                   , CGRectGetHeight(_addMarkBtn.bounds));
    [_deleteProductBtn setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteProductBtn setBackgroundImage:releteBackgroundImage forState:UIControlStateNormal];
    _deleteProductBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_deleteProductBtn.layer setMasksToBounds:YES];
    [_deleteProductBtn.layer setCornerRadius:4.0];
    [_deleteProductBtn addTarget:self action:@selector(releteProductEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_deleteProductBtn];
    if (!isEdit) {
        _addMarkBtn.hidden = YES;
        _deleteProductBtn.hidden = YES;
    }
}
- (void)changeSubViewShowWithEditStatus:(BOOL)isEdit {
    
    if (isEdit) {
        _addMarkBtn.hidden = NO;
        _deleteProductBtn.hidden = NO;
        _totalPricelabel.hidden = YES;
        _goJieSuanBtn.hidden = YES;
    } else {
        _addMarkBtn.hidden = YES;
        _deleteProductBtn.hidden = YES;
        _totalPricelabel.hidden = NO;
        _goJieSuanBtn.hidden = NO;
    }
}
#pragma mark -- button Action
- (void)selectAllProductEvent:(id)sender {
    UIButton *button = (UIButton*)sender;
    button.selected = !button.selected;
    if (_delegate && [_delegate respondsToSelector:@selector(selectedAllProductsWithSelectedStatus:)]) {
        [_delegate selectedAllProductsWithSelectedStatus:button.selected];
    }
}

- (void)goJieSuanEvent:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(goJieSuan)]) {
        [_delegate goJieSuan];
    }
}
- (void)addMarkEvent:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(addGuanZhu)]) {
        [_delegate addGuanZhu];
    }
}

- (void)releteProductEvent:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(deleteProduct)]) {
        [_delegate deleteProduct];
    }
}

#pragma mark -- setter method
- (void)setIsAllSelected:(BOOL)isAllSelected {
    self.selectedAllProductBtn.selected = isAllSelected;
    _isAllSelected = isAllSelected;
}

- (void)setTotalPrice:(NSString *)totalPrice {
    NSString *appendStr = @"总计:￥";
    self.totalPricelabel.text = [NSString stringWithFormat:@"%@ %@",appendStr,totalPrice];
}

- (void)setProductCount:(NSString *)productCount {
    NSString *title = [NSString stringWithFormat:@"去结算(%@)",productCount];
    [self.goJieSuanBtn setTitle:title forState:UIControlStateNormal];
}
@end
